"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var uuid_1 = require("uuid");
var types_1 = require("./types");
var hop = Object.prototype.hasOwnProperty;
function isBoolExpression(exp) {
    return hop.call(exp, 'args');
}
exports.isBoolExpression = isBoolExpression;
/**
 * 给一个表达式中每一个bool节点增加uuid
 *
 * @param {Expression} exp - 表达式的树根节点
 */
function makeSureUUID(exp, forceUpdate) {
    if (forceUpdate === void 0) { forceUpdate = false; }
    if (isBoolExpression(exp)) {
        exp.args.forEach(function (i) { return makeSureUUID(i, forceUpdate); });
    }
    else {
        if (exp.uuid === undefined || forceUpdate) {
            exp.uuid = uuid_1.v4();
        }
        if (exp.operator === types_1.Operator.SEGMENTS_INNER && Array.isArray(exp.value)) {
            exp.value.forEach(function (i) { return makeSureUUID(i, forceUpdate); });
        }
    }
}
exports.makeSureUUID = makeSureUUID;
/**
 * 将树形结构改成Map
 *
 * @param {Expression} exp - 表达式树根
 * @return {Map<string, BoolItem>} map
 */
function buildBoolItemMap(exp) {
    var itemMap = new Map();
    var queue = [exp];
    while (queue.length > 0) {
        var ite = queue.shift();
        if (isBoolExpression(ite)) {
            queue = queue.concat.apply(queue, ite.args);
        }
        else {
            itemMap.set(ite.uuid, ite);
        }
    }
    return itemMap;
}
exports.buildBoolItemMap = buildBoolItemMap;
